package com.solution.admin.product.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.solution.admin.product.entity.ProductCategoryPropertyFilterEntity;
import com.solution.admin.product.service.ProductCategoryPropertyFilterService;
import com.solution.common.controller.BaseController;
import com.solution.common.entity.BaseReturn;
import com.solution.common.page.JSONGrid;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;
import com.solution.common.utils.string.StringUtil;

/**
 * 商品分类搜索筛选项web控制层
 * @author lilp
 * @date 2017-10-09 23:17:28
 */
@RequestMapping("/productCategoryPropertyFilter")
@Controller
public class ProductCategoryPropertyFilterController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(ProductCategoryPropertyFilterController.class);

	@Autowired
	private ProductCategoryPropertyFilterService productCategoryPropertyFilterService;
	
	/**
	 * 跳转商品分类搜索筛选项查询页面
	 * @param ProductCategoryPropertyFilter 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-10-09 23:17:28
	 */
	@RequestMapping("/toListProductCategoryPropertyFilterPage")
	public String toListProductCategoryPropertyFilterPage(ProductCategoryPropertyFilterEntity productCategoryPropertyFilter, HttpServletRequest request) {
		return "product/productCategoryPropertyFilter_list";
	}

	/**
	 * 分页查询商品分类搜索筛选项列表
	 * @param productCategoryPropertyFilter 接收界面查询条件参数
	 * @param request 请求对象
	 * @return 返回对应的页面视图
	 * @author lilp
 	 * @date 2017-10-09 23:17:28
	 */
	@RequestMapping("/listProductCategoryPropertyFilterPage")
	@ResponseBody
	public JSONGrid listProductCategoryPropertyFilterPage(ProductCategoryPropertyFilterEntity productCategoryPropertyFilter, HttpServletRequest request){
		int pageNum=Integer.parseInt(request.getParameter("page").toString());
		int numPerPage=Integer.parseInt(request.getParameter("rows").toString());
		PageParam pageParam = super.getPageParam(request);
		pageParam.setPageNum(pageNum);
		pageParam.setNumPerPage(numPerPage);
		
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("productCategoryId", productCategoryPropertyFilter.getProductCategoryId());
		try {
			PageBean pageBean = productCategoryPropertyFilterService.listProductCategoryPropertyFilterPage(pageParam, paramMap);
			return new JSONGrid(pageBean.getRecordList(),pageBean.getTotalCount());
		} catch (Exception e) {
			handleException("分页查询商品分类搜索筛选项失败", logger, e);
		}
		return new JSONGrid();
	}
	
	
	/**
	 * 跳转新增页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-10-09 23:17:28
	 */
	@RequestMapping("/toAddProductCategoryPropertyFilter")
	public String toAddProductCategoryPropertyFilter(HttpServletRequest request) {
		request.setAttribute("opFlag", "add");
		return "product/productCategoryPropertyFilter_form";
	}

	/**
	 * 跳转编辑页面
	 * @return string 跳转页面路径
	 * @author lilp
 	 * @date 2017-10-09 23:17:28
	 */
	@RequestMapping("/toUpdateProductCategoryPropertyFilter")
	public String toUpdateProductCategoryPropertyFilter(HttpServletRequest request) {
		// 编辑操作
		request.setAttribute("opFlag", "edit");
		long id = Long.valueOf(request.getParameter("id"));
		try {
			ProductCategoryPropertyFilterEntity productCategoryPropertyFilter = productCategoryPropertyFilterService.getProductCategoryPropertyFilterById(id);
			request.setAttribute("productCategoryPropertyFilter", productCategoryPropertyFilter);
		} catch (Exception e) {
			handleException("跳转编辑商品分类搜索筛选项页面错误", logger, e);
		}
		return "product/productCategoryPropertyFilter_form";
	}


	/**
	 * 新增或保存商品分类搜索筛选项
	 * @param request 请求对象
	 * @param productCategoryPropertyFilterEntity 商品分类搜索筛选项实体
	 * @return
	 * @author lilp
 	 * @date 2017-10-09 23:17:28
	 */
	@RequestMapping("/saveOrUpdateProductCategoryPropertyFilter")
	@ResponseBody
	public Map<String, Object> saveProductCategoryPropertyFilter(HttpServletRequest request, ProductCategoryPropertyFilterEntity productCategoryPropertyFilterEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			String id = request.getParameter("id");
			if(StringUtil.isNotEmpty(id)){
				result = productCategoryPropertyFilterService.updateProductCategoryPropertyFilter(productCategoryPropertyFilterEntity);
			}else{
				productCategoryPropertyFilterEntity.setCreater(this.getUserName());
				result = (int)productCategoryPropertyFilterService.saveProductCategoryPropertyFilter(productCategoryPropertyFilterEntity);
				resultMap.put("id", result);
			}
		} catch (Exception e) {
			handleException("添加商品分类搜索筛选项失败", logger, e);
		}
		resultMap.put("code", result);
		return resultMap;
	}
	
	@RequestMapping("/removeProductCategoryPropertyFilter")
	@ResponseBody
	public Map<String, Object> removeProductCategoryPropertyFilter(HttpServletRequest request, ProductCategoryPropertyFilterEntity productCategoryPropertyFilterEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int result = 0;
		try {
			result = productCategoryPropertyFilterService.deleteProductCategoryPropertyFilterById(productCategoryPropertyFilterEntity.getId());
		} catch (Exception e) {
			handleException("添加商品分类搜索筛选项失败", logger, e);
		}
		resultMap.put("code", result);
		return resultMap;
	}
	
}
